<?php
// Copyright 2008 Datapolis Solutions OY
// 
// This file is part of a program, that is distributed
// under the terms of the GNU General Public License,
// see license.txt for details.

//
//	Datapolis Solutions Oy
//
//notw: defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
//notw: require_once( $mainframe->getPath( 'front_html' ) );

// include base classes (actually already included in 'dpposition.html.php' file)

//require_once($mosConfig_absolute_path."/components/com_dpposition/dpposition.base.classes.php");
require_once(JPATH_COMPONENT."/dpposition.base.classes.php");


// include class file

require_once(JPATH_COMPONENT."/dpposition.class.php");
require_once(JPATH_COMPONENT."/DPEventSchedule.php");
require_once(JPATH_COMPONENT."/dpdatabase.class.php");
require_once(JPATH_COMPONENT."/dpposition.html.php");

// include language file
$dpposition_language_dir = JPATH_COMPONENT."/com_dpposition/languages";
if( file_exists("${dpposition_language_dir}/${mosConfig_lang}.php") ){
	require_once("${dpposition_language_dir}/${mosConfig_lang}.php");
}else if( file_exists("${dpposition_language_dir}/finnish.php") ){
	require_once("${dpposition_language_dir}/finnish.php");
}

// create view
$dpposition_view = new dppositionView();

// create controller
$dpposition_ctrl = new dppositionController($mainframe, $database, $my, $option, $task, $Itemid);

// set view to controller and vice versa
$dpposition_ctrl->setView($dpposition_view);
$dpposition_view->setController($dpposition_ctrl);

// frontend editing
$dpposition_ctrl->frontendParamsEditorAllowed(false);
$dpposition_ctrl->addFrontendParamsEditorUsertype('super administrator');
$dpposition_ctrl->addFrontendParamsEditorUsertype('administrator');
$dpposition_ctrl->addFrontendParamsEditorUsertype('manager');

// load component parameters
if( $dpposition_ctrl->loadParameters(false) && $dpposition_ctrl->openSharedDB() )
{

	// task protector
	$ok = true;
	$publicTasks = array("gettags", "gettaggedpositions", "getposition");
	if( !in_array($task, $publicTasks) ){
		$adminusergroups = strtolower($dpposition_ctrl->_params->get("adminusergroups", ""));
		if( !in_array(strtolower($my->usertype), explode(',', $adminusergroups)) || empty($adminusergroups) ){
			$ok = false;
			echo "Unauthorized access!";
		}
	}
	
	// execute task
	if( $ok ){
		switch ($task)
		{
			case "gettags":
				$dpposition_ctrl->taskGetTags();
				break;
			
			case "getaliases":
				$dpposition_ctrl->taskGetAliases();
				break;
			
			case "gettaggedpositions":
				$dpposition_ctrl->taskGetTaggedPositions();
				break;
			
			case "getposition":
				
				$dpposition_ctrl->taskGetPosition();
				break;
			
			case "getpositionaliases":
				$dpposition_ctrl->taskGetPositionAliases();
				break;
			
			case "getpositiontags":
				$dpposition_ctrl->taskGetPositionTags();
				break;
			
			case "showpositions":
				$dpposition_ctrl->taskShowPositions();
				break;
			
			case "setaliases":
				$dpposition_ctrl->taskSetAliases();
				break;
			
			case "settags":
				$dpposition_ctrl->taskSetTags();
				break;
			
			case "saveposition":
				$dpposition_ctrl->taskSavePosition();
				break;
			
			case "deleteposition":
				$dpposition_ctrl->taskDeletePosition();
				break;
			
			case "showaliases":
				$dpposition_ctrl->taskShowAliases();
				break;
			
			case "deletealias":
				$dpposition_ctrl->taskDeleteAlias();
				break;
			
			case "savealias":
				$dpposition_ctrl->taskSaveAlias();
				break;
			
			case "showtags":
				$dpposition_ctrl->taskShowTags();
				break;
			
			case "newdistrict":
				$dpposition_ctrl->taskNewDistrict();
				break;
			
			case "deletetag":
				$dpposition_ctrl->taskDeleteTag();
				break;
			
			case "savetag":
				$dpposition_ctrl->taskSaveTag();
				break;
			
			/*
			case "tmpedit":
				$dpposition_ctrl->taskTmpEdit();
				break;
			
			case "tmpsave":
				$dpposition_ctrl->taskTmpSave();
				break;
			
			case "tmpshow":
				$dpposition_ctrl->taskTmpShow();
				break;
			
			case "paramsView":
				$dpposition_ctrl->taskParamsEdit();
				break;
			
			case "paramsEdit":
				$dpposition_ctrl->taskParamsEdit();
				break;
			
			case "paramsSave":
				$dpposition_ctrl->paramsSave();
				break;
			*/
			default:
				$dpposition_ctrl->taskShowPositions();
				break;
		}
	}
}
